package com.hospital.search.module.controller;

import com.hospital.common.base.PageResult;
import com.hospital.common.base.Result;
import com.hospital.common.dto.search.module.PostInsertDto;
import com.hospital.common.dto.search.module.PostQueryDto;
import com.hospital.common.dto.search.module.ReplyInsertDto;
import com.hospital.common.vo.search.module.IndexRecordVo;
import com.hospital.common.vo.search.module.PostIndexVo;
import com.hospital.search.module.base.MessageType;
import com.hospital.search.module.base.RollbackInfo;
import com.hospital.search.module.message.Message;
import com.hospital.search.module.message.producer.ElasticInsertProducer;
import com.hospital.search.module.service.SearchService;
import com.hospital.search.module.service.TIndexRecordService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 搜索引擎相关接口
 * @author yhn
 * @version 1.0
 */
@RestController
@RequestMapping("/elastic")
public class ElasticController {
    @Autowired
    private SearchService searchService;
    @Autowired
    private ElasticInsertProducer elasticInsertProducer;
    @Autowired
    private TIndexRecordService indexRecordService;

    /**
     * 插入帖子到ES中
     */
    @PostMapping("/insertPost")
    public Result<?> insertPost(@Valid @RequestBody PostInsertDto insertDto){
        elasticInsertProducer.send(new Message(MessageType.POST, insertDto));
        return Result.success();
    }
    /**
     * 插入回复到ES中
     */
    @PostMapping("/insertReply")
    public Result<?> insertReply(@Valid @RequestBody ReplyInsertDto insertDto){
        elasticInsertProducer.send(new Message(MessageType.REPLY, insertDto));
        return Result.success();
    }
    /**
     * 搜索帖子
     */
    @PostMapping("/search")
    public Result<PageResult<PostIndexVo>> search(@RequestBody PostQueryDto queryDto){
        return Result.success(searchService.search(queryDto));
    }
    /**
     * 回滚索引
     */
    @GetMapping("/rollback")
    public Result<RollbackInfo> rollback(){
        return Result.success(indexRecordService.rollBackRecentIndex());
    }

    /**
     * 查询索引
     */
    @GetMapping("/findAll")
    public Result<PageResult<IndexRecordVo>> findAll(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize){
        return Result.success(indexRecordService.findAll(pageNum,pageSize));
    }
}
